﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Domain;
using System.Data;

namespace MyVoteMVC.Data.Repositories
{
    public class ActivityRepository : Repository<VT_Activities>
    {
        public override bool Add(VT_Activities entity)
        {
            if (entity.ActivityImage == null)
            {
                var image = new VT_ActivityImage() { Activity_ID = entity.Activity_ID };
                ctx.Entry<VT_ActivityImage>(image).State = EntityState.Added;
            }

            return base.Add(entity);
        }

        public override bool Delete(VT_Activities entity)
        {
            if (entity.ChildActivities != null)
            {
                List<VT_Activities> activities = entity.ChildActivities.ToList();
                foreach (VT_Activities activity in activities)
                    Delete(activity);
            }
            if (entity.ActivityImage == null)
            {
                var image = new VT_ActivityImage() { Activity_ID = entity.Activity_ID };

                if (ctx.Entry<VT_ActivityImage>(image).State == EntityState.Detached)
                {
                    ctx.ActivityImages.Attach(image);
                }
            }
            return base.Delete(entity);




        }
    }
}
